package com.smec.moblie.view.dialog;

/**
 * Created by sunyubo on 16/3/3.
 */

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.control.EventCenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 年月选择Dialog
 *
 * @author kn
 *
 */
public class MonthSelectDialog {

    private Context context;
    private Dialog dialog;
    private List<HashMap<String, String>> monthList;

    private String sel_month;

    public MonthSelectDialog(Context context) {

        this.context = context;
        dialog = new Dialog(context);
    }

    @SuppressWarnings("deprecation")
    public String show() {

        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_monthselector, null);
        dialog.setContentView(view);
        dialog.setTitle("选择查看月份");

        ListView listview_monthselect = (ListView) view
                .findViewById(R.id.listview_monthselect);
        monthList = getMonthList();

        // 动态设定listView宽度
        // LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        // LinearLayout.LayoutParams.MATCH_PARENT,
        // monthList.size() * 50 - 1);
        // listview_monthselect.setLayoutParams(lp);

        listview_monthselect.setAdapter(new MonthSelectListAdapter(context,
                monthList));
        listview_monthselect
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent,
                                            View view, int position, long id) {
                        sel_month = monthList.get(position)
                                .get("MonthValue").toString();
                        dialog.dismiss();
                        EventBus.getDefault().post(new EventCenter.SelectMonth(sel_month));
                    }
                });

        dialog.show();
        WindowManager wm = ((Activity)context).getWindowManager();

        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        dialog.getWindow().setLayout(width * 9 / 10, height * 5 / 10);
        return sel_month;
    }

    private class MonthSelectListAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private List<HashMap<String, String>> monthList;

        public MonthSelectListAdapter(Context context,
                                      List<HashMap<String, String>> monthList) {
            this.inflater = LayoutInflater.from(context);
            this.monthList = monthList;
        }

        @Override
        public int getCount() {
            return monthList.size();
        }

        @Override
        public Object getItem(int position) {
            return this.monthList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = inflater.inflate(
                    R.layout.dialog_monthselectormain, null);

            TextView myTextView = null;
            myTextView = (TextView) convertView
                    .findViewById(R.id.textview_monthselect);
            myTextView.setText(monthList.get(position).get("MonthName")
                    + "");// 月份

            return convertView;
        }

    }

    /**
     * 获取最近一年的Month列表
     *
     * @return
     */
    private List<HashMap<String, String>> getMonthList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM");
        List<HashMap<String, String>> returnList = new ArrayList<HashMap<String, String>>();

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.add(Calendar.MONTH, 1);// 从下一个月开始

        HashMap<String, String> allMonthMap = new HashMap<String, String>();
        allMonthMap.put("MonthName", "全部");
        allMonthMap.put("MonthValue", "ALL");
        returnList.add(allMonthMap);

        for (int i = 0; i < 4; i++) {
            HashMap<String, String> monthMap = new HashMap<String, String>();
            if (i == 0) {
                monthMap.put("MonthName", sdf.format(cal.getTime()));
                monthMap.put("MonthValue", sdf2.format(cal.getTime()));
            } else {
                cal.add(Calendar.MONTH, -1);
                monthMap.put("MonthName", sdf.format(cal.getTime()));
                monthMap.put("MonthValue", sdf2.format(cal.getTime()));
            }
            returnList.add(monthMap);
        }

        return returnList;
    }

}
